package serviceImpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import DAO.BaseDAO;
import DTO.Lb;
import pageUtil.PageB;
import util.DBConnection;
import util.DBConnection.CallBack;

public class LbDAO implements BaseDAO<Lb> {

	@Override
	public boolean add(Lb lb, String sql) throws Exception {
		Connection conn = DBConnection.getConn();
		boolean boo = DBConnection.executUpadte(conn, sql, lb.getLbcs(), lb.getLbname(), lb.getCjsj());
		if (conn != null) {
			conn.close();
		}
		return boo;
	}

	@Override
	public boolean delete(Lb lb) throws Exception {
		String sql = "delete from lb where id = ?";
		Connection conn = DBConnection.getConn();
		boolean boo = DBConnection.executUpadte(conn, sql, lb.getId());
		if (conn != null) {
			conn.close();
		}
		return boo;
	}

	@Override
	public boolean update(Lb lb, String sql) throws Exception {
		Connection conn = DBConnection.getConn();
		boolean boo = DBConnection.executUpadte(conn, sql, lb.getLbname(),lb.getLbcs());
		if (conn != null) {
			conn.close();
		}
		return boo;
	}

	@Override
	public List<Lb> findAll(int currentPage, int pageSize, String sql) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Lb> findAll(String sql) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Lb findById(String id) throws Exception {
		String sql = "select id,lbname,lbcs,cjsj from lb where id=?";
		return DBConnection.queryOne(new CallBack<Lb>() {
			@Override
			public Lb getData(ResultSet rs) {
				Lb lb = null;
				try {
					if (rs.next()) {
						lb = new Lb();
						lb.setId(rs.getInt("id"));
						lb.setLbcs(rs.getString("lbcs"));
						lb.setLbname(rs.getString("lbname"));
						lb.setCjsj(rs.getString("cjsj"));
					}
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					if (rs != null) {
						try {
							rs.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
				return lb;
			}

		}, sql, id);
	}

	
	/**
	 * 校验重复问题
	 */
	@Override
	public Lb findByPropert(Lb t) throws Exception {
		String sql = "select count(id) as count from lb where lbcs = ?";
		return DBConnection.queryOne(new CallBack<Lb>() {
            @Override
            public Lb getData(ResultSet rs) {
            	Lb lb = null;
                try {
                    if(rs.next()){
                        lb = new Lb();
                        lb.setLbcs(String.valueOf(rs.getInt("count")));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs!= null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return lb;
            }


        }, sql, t.getLbcs());
	}

	@Override
	public int dataCount(String sql) throws Exception {
		return DBConnection.queryOne(new CallBack<Integer>() {
			@Override
			public Integer getData(ResultSet rs) {
				int count = 0;
				try {
					if (rs.next()) {
						count = rs.getInt(1);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					if (rs != null) {
						try {
							rs.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
				return count;

			}
		}, sql);
	}

	@Override
	public List<Lb> findByPage(PageB<Lb> bean, String sql) throws SQLException {
		int start = (bean.getCurrentPage() - 1) * bean.getPageSize();
		return DBConnection.queryList(new CallBack<Lb>() {

			@Override
			public List<Lb> getDatas(ResultSet rs) {
				List<Lb> list = new ArrayList<Lb>();
				Lb lb = null;
				try {
					while (rs.next()) {
						lb = new Lb();
						lb.setId(rs.getInt("id"));
						lb.setLbcs(rs.getString("lbcs"));
						lb.setLbname(rs.getString("lbname"));
						lb.setCjsj(rs.getString("cjsj"));
						list.add(lb);
					}
				} catch (SQLException e) {
					System.out.println(e.getMessage());
				} finally {
					if (rs != null) {
						try {
							rs.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
				return list;
			}
		}, sql, start, bean.getPageSize());
	}
}
